package dao.admin.impl;

import dao.BaseDao;
import dao.admin.AdminDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * AdminDaoImpl
 *
 * @author 谭文斌
 * @since 2023/8/29
 */
public class AdminDaoImpl extends BaseDao implements AdminDao {
    /**
     * 管理员登录
     * @param username 账号
     * @param password 密码
     * @return
     */
    @Override
    public String login(String username, String password) {
        if (username.trim().equals("")){
            return "账号不能为空";
        }
        if (password.trim().equals("")){
            return "密码不能为空";
        }
        int row=0;
        String sql="select count(*) c from admin where username=? and password=?";
        Connection conn = super.getConnection();
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            ps = conn.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            rs = ps.executeQuery();
            while (rs.next()) {
                row= rs.getInt("c");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            closeAll(rs,ps,conn);
        }
        return row == 0 ? "账号或密码错误！" : String.valueOf(row);
    }
}
